package org.example.user.warning.repository;

import org.example.commons.entity.SysFeedback;
import org.example.commons.entity.SysMessage;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @author bothin
 */
@Repository
public interface SysMessageRepository extends JpaRepository<SysMessage,Long> {

    @Query("select m from SysMessage m " +
            "where m.userId = :userId " +
            "and m.sendTo in ('1','2') " +
            "and m.status = :status")
    Page<SysMessage> findUserMessage(Long userId, String status, Pageable pageable);

    @Query("select m from SysMessage m " +
            "where m.userId = :userId " +
            "and m.sendTo in ('1','2') ")
    Page<SysMessage> findUserMessage(Long userId,  Pageable pageable);

    @Query("select m from SysMessage m " +
            "where  m.sendTo in ('0','2') " +
            "and m.status = :status")
    Page<SysMessage> findBackendMessage(String status, Pageable pageable);

    @Query("select m from SysMessage m " +
            "where  m.sendTo in ('0','2') " +
            "and m.status = :status")
    List<SysMessage> findBackendByStatusMessage(String status);

    /**
     * 查询后台所有消息
     * @param pageable
     * @return
     */
    @Query("select m from SysMessage m " +
            "where  m.sendTo in ('0','2') ")
    Page<SysMessage> findBackendMessage(Pageable pageable);

    /**
     * 查询后台所有消息
     * @return
     */
    @Query("select m from SysMessage m " +
            "where  m.sendTo in ('0','2') ")
    List<SysMessage> findBackendMessage();

    List<SysMessage> findByMessageIdIn(Long[] ids);

    /**
     * 条件查询
     * @return
     */
    @Query(value = "select m from SysMessage m " +
            "where m.sendTo in ('0','2') and (:status IS NULL OR m.status = :status)  " +
            " and m.userName like :name and m.title like :title " +
            " and ( :#{#dates[0]} IS NULL OR :#{#dates[1]} IS NULL " +
            " OR m.createTime between :#{#dates[0]} and :#{#dates[1]}) ")
    Page<SysMessage> searchList(String name, String title, Date[] dates, String status, Pageable pageable);
}
